<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: SendMailPortlet.php 412 2011-01-04 21:21:57Z steen.rabol $
 * @package Pradoportal.Porttlets.SendMailPortlet.Edit
 *
 */
 Prado::using('Application.Portlets.Portlet');

class SendMailPortlet extends Portlet
{
	private $_userRecord	= null;

	public function  getConfigName()
	{
		return get_class();
	}

	public function OnLoad($parm)
	{
		parent::OnLoad($parm);
		$this->bindData();
	}


	public function bindData()
	{
		$prop = @unserialize($this->ConfigRecord->value);
		if(is_array($prop))
		{
			if(isset ($prop['legendtext']))
				$this->SendMailLegendText->Text = $prop['legendtext'];
			if(isset ($prop['showlegend']) && TPropertyValue::ensureInteger($prop['showlegend']) == 1)
				$this->SendMailShowLegend->Checked=true;
		}
		$this->dlRecipients->DataSource = $this->Recipients;
		$this->dlRecipients->VirtualItemCount=count($this->Recipients);
		$this->dlRecipients->dataBind();
	}

	public function getUserRecord()
	{
		return $this->_userRecord;
	}

	public function cancel($sender, $parm)
	{
		$this->Page->gotoDefaultPage();
	}

	public function createRecipient($sender, $parm)
	{
		if($this->Page->IsValid)
		{
			$this->DataAccess->createCommand("insert into tblsendemailportlet (name,email,active) values('" . $this->Name->Text . "','" . $this->Email->Text . "','" . TPropertyValue::ensureInteger($this->Active->Checked) . "')")->execute();
			$this->bindData();
		}
	}

	public function getRecipients()
	{
		return $this->Page->DataAccess->createCommand("select * from tblsendemailportlet")->query();
	}

	public function editItem($sender,$param)
	{
		$this->dlRecipients->SelectedItemIndex = -1;
		$this->dlRecipients->EditItemIndex	= $param->Item->ItemIndex;
		$this->bindData();
	}

	public function cancelItem($sender,$param)
	{
		$this->dlRecipients->SelectedItemIndex	= -1;
		$this->dlRecipients->EditItemIndex		= -1;
		$this->bindData();
	}

	public function updateItem($sender,$param)
	{
		$item	= $param->Item;
		$id		= $this->dlRecipients->DataKeys[$item->ItemIndex];
		$name	= $item->Name->Text;
		$email	= $item->Email->Text;
		$active	= TPropertyValue::ensureInteger($item->Active->Checked);
		$this->DataAccess->createCommand("update tblsendemailportlet set name='$name',email='$email',active=$active where id=$id")->execute();
		$this->dlRecipients->EditItemIndex=-1;
		$this->bindData();
	}

	public function deleteItem($sender,$param)
	{
		$id = $this->dlRecipients->DataKeys[$param->Item->ItemIndex];
		$this->Page->DataAccess->createCommand("delete from tblsendemailportlet where id=$id")->execute();
		$this->dlRecipients->SelectedItemIndex=-1;
		$this->dlRecipients->EditItemIndex=-1;
		$this->bindData();
	}

	public function SaveSendMailOptionsBtnClicked($sender, $param)
	{
		$value		= serialize(array("showlegend" => TPropertyValue::ensureInteger($this->SendMailShowLegend->Checked), "legendtext" => TPropertyValue::ensureString($this->SendMailLegendText->Text)));
		$this->ConfigRecord->value  = $value;
		$this->ConfigRecord->save();
	}

}

?>